 #!/usr/bin/python
 # -*- coding: iso-8859-15 -*-
import os
import sys

def isNotNull(value):
    return value is not None and len(value) > 0

fileList = []
rootdir = sys.argv[1]
for root, subFolders, files in os.walk(rootdir):
    for file in files:
       	fileList.append(os.path.join(root,file))
for file in fileList:
	respuestas = False
	caseId = file.split("/")[-1]
	htmlfile = open("/media/data/sarhtml/"+caseId+".html","w")
	htmlfile.write("""<html><head><style type="text/css">
	.subtitle{
	font-family: Arial;
	font-size: 13pt;
	font-weight: bold;	
	margin-left: 5px;
	color: #3C6E83;
	}
	.entry{
	font-family: Arial;
	font-size: 10pt;
	margin-left: 25px;
	display: inline-block;
	text-align: justify;
	}
	.entry_t{
	font-family: Arial;
	font-size: 10pt;
	font-weight: bold;	
	margin-left: 25px;
	}
	.entry_v{
	font-family: Arial;
	font-size: 10pt;
	margin-left: 2px;
	display: inline-block;
	text-align: justify;
	}
	</style>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body>""")
	htmlfile.write("<spam class=\"subtitle\" ><a style=\"color:#333366;font-size: 16pt;font-weight: bold;\" href=\"" +caseId+"\">"+caseId.split(".")[0]+"</a></spam><br/>")
	txtfile = open(file, "r").readlines()
	texto = titulo = valor = None
	for line in txtfile:
		# line=line.encode("utf-8")
		# line=uline.encode("latin-1","ignore")
		if " : " in line: 		
	   		if line.startswith("texto : "):
	   			texto = line.split(" : ")[1].replace("\n", "")
	   			if respuestas: #Texto para respuestas
					htmlfile.write("""<spam class="entry">"""+texto.capitalize().replace("\n"," ")+"""</spam><br/>""")
	   		elif line.startswith("titulo : "):
	   			titulo = line.split(" : ")[1].replace("\n", "")
	   			if respuestas: #Titulo para respuestas
					htmlfile.write("""<spam class="entry">"""+titulo.capitalize().replace("\n"," ")+"""</spam><br/>""")
	   		elif line.startswith("valor : "):
	   			valor = line.split(" : ")[1].replace("\n", "")
	   			if isNotNull(texto) and isNotNull(titulo):#Titulo-Valor-Texto
	   				htmlfile.write("""<spam class="entry_t">"""+titulo+""": </spam>""")
	   				htmlfile.write("""<spam class="entry_v">"""+valor+" "+texto+"""</spam><br/>""")
	   				texto = titulo = valor = None
	   			elif isNotNull(titulo):#Titulo-Valor
	   				htmlfile.write("""<spam class="entry_t">"""+titulo+""": </spam>""")
	   				htmlfile.write("""<spam class="entry_v">"""+valor+"""</spam><br/>""")
	   				titulo = valor = None
	   		elif isNotNull(texto) and isNotNull(titulo) and not respuestas:
				htmlfile.write("""<spam class="entry">"""+titulo.capitalize().replace("\n"," ")+"""</spam><br/>""")
				htmlfile.write("""<spam class="entry">"""+texto.capitalize().replace("\n"," ")+"""</spam><br/>""")
				texto = titulo = None
	   		else:
	   			title = line.split(" : ")[0]
	   			value = line.split(" : ")[1]
	   			if title.startswith("entrada"):
	   				htmlfile.write("""<spam class="entry_t"></spam>""")
	   			else:
	   				htmlfile.write("""<spam class="entry_t">"""+title.capitalize().replace("\n", "")+""": </spam>""")	
	   			htmlfile.write("""<spam class="entry_v">"""+value.capitalize().replace("\n", "")+"""</spam><br/>""")
	   			title = value = None
		elif line.startswith("RESPUESTA") and not respuestas:
	   		htmlfile.write("""<spam class="subtitle">RESPUESTAS</spam><br/>""")
	   		respuestas = True
		else:
			if isNotNull(texto) and not respuestas:
				htmlfile.write("""<spam class="entry_t">Descripción: </spam>""")
				htmlfile.write("""<spam class="entry">"""+texto.capitalize().replace("\n"," ")+"""</spam><br/>""")
				snnipetfile = open("/media/data/sarhtml/"+caseId+".snippet","w")
				snnipetfile.write(texto.capitalize().replace("\n"," "));
				snnipetfile.close();
				texto = None
			if  isNotNull(titulo) and not respuestas:
				htmlfile.write("""<spam class="entry">"""+titulo.capitalize().replace("\n"," ")+"""</spam><br/>""")
				titulo = None	
			if not respuestas:	
	   			htmlfile.write("""<spam class="subtitle">"""+line.upper().replace("_", " ").replace("\n", "")+"""</spam><br/>""")
	htmlfile.write("""</body></html>""")   		
	htmlfile.close()   		
